extends MarginContainer

@onready var faction_list_root: VBoxContainer = $MarginContainer/VBoxContainer/HBoxContainer/ScrollContainer/FactionListVBoxContainer
@onready var sure_button:Button = $MarginContainer/VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/HBoxContainer/SureButton
@onready var king_name_label: Label = $MarginContainer/VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer/VBoxContainer/TextureRect/KingNameLabel
@onready var city_count_label: Label = $MarginContainer/VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2/VBoxContainer/HBoxContainer2/CityCountLabel
@onready var hero_count_label: Label = $MarginContainer/VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2/VBoxContainer/HBoxContainer2/HeroCountLabel
@onready var soldier_count_label: Label = $MarginContainer/VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2/VBoxContainer/HBoxContainer2/SoldierCountLabel
@onready var gold_value_label: Label = $MarginContainer/VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2/VBoxContainer/HBoxContainer3/GoldValueLabel
@onready var food_value_label: Label = $MarginContainer/VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2/VBoxContainer/HBoxContainer3/FoodValueLabel
@onready var population_value_label: Label = $MarginContainer/VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2/VBoxContainer/HBoxContainer3/PopulationValueLabel

var _fation_list_item_prefab = preload("res://Prefebs/Components/Faction/faction_list_item.tscn")

var faction_index:int=0

func _ready():
	sure_button.pressed.connect(_on_sure_button_clicked)
	
	_generate_faction_list_item()
	_first_check()
	pass
	
func _first_check():
	var children = faction_list_root.get_children()
	if children.size() == 0:
		return
	(children[0] as FactionListItem).check_faction()
	_faction_check_change(children[0])
	pass
	
func _faction_check_change(item:FactionListItem):
	var king_id = item.faction_entity.king_id
	var king : HeroEntity = item.faction_entity.get_king()
	king_name_label.text = king.hero_name
	city_count_label.text = var_to_str(item.faction_entity.total_city)
	hero_count_label.text = var_to_str(item.faction_entity.total_hero)
	soldier_count_label.text = var_to_str(item.faction_entity.total_soldiers)
	gold_value_label.text = var_to_str(item.faction_entity.total_gold)
	food_value_label.text = var_to_str(item.faction_entity.total_food)
	population_value_label.text = var_to_str(item.faction_entity.total_population)
	GlobalConfig.selected_faction = item.faction_entity
	pass
	
func _generate_faction_list_item():
	var children = faction_list_root.get_children()
	if children.size() != 0:
		for item in children:
			faction_list_root.remove_child(item)
			pass
		pass
	var list = GlobalConfig.faction_list
	var i = 0
	for item:FactionEntity in list:
		var instance_item = _fation_list_item_prefab.instantiate()
		faction_list_root.add_child(instance_item)
		(instance_item as FactionListItem).faction_checked.connect(_on_faction_item_checked)
		instance_item.set("faction_entity",item)
		instance_item.set("faction_index",i)
		i+=1
		pass
	pass

func _on_faction_item_checked(item:FactionListItem):
	faction_index = item.faction_index
	_faction_check_change(item)
	pass
	
func _on_sure_button_clicked():
	get_tree().change_scene_to_file("res://Scences/big_map.tscn")
	pass
